import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import Admin from '../views/Admin/index.vue'
import Dash from '../views/Admin/Dash.vue'
import Orderlist from '../views/Admin/Orderlist.vue'
import User from '../views/Admin/User.vue'
import Payset from '../views/Admin/Payset.vue'
import Login from '../views/Admin/Login.vue'
import NoMath from '../views/Admin/NoMath.vue'
import goodsSet from '../views/Admin/goodsSet.vue'
import Shop from '../views/Admin/Shop.vue'
import Submit from '../views/Admin/Submit.vue'
import Addgoods from '../views/Admin/Addgoods.vue'
import Refund from '../views/Admin/Refund.vue'
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '*',
    name: 'NoMath',
    component: NoMath
  },
  {
    path: '/admin',
    name: 'Admin',
    component: Admin,
    children:[
	 {path:"dash",name:"首页" ,component:Dash,meta:{requireAuth:true}},	
   {path:"orderlist",name:"订单列表", component:Orderlist,meta:{requireAuth:true}},
	 {path:"user",name:"用户管理",component:User,meta:{requireAuth:true}},
	 {path:"payset",name:"支付设置",component:Payset,meta:{requireAuth:true}},
	 {path:"goodsset",name:"商品设置",component:goodsSet,meta:{requireAuth:true}},
	 {path:"shop",name:"门店管理",component:Shop,meta:{requireAuth:true}},
	 {path:"submit",name:"发布商品",component:Submit,meta:{requireAuth:true}},
	 {path:"addgoods",name:"添加商品",component:Addgoods,meta:{requireAuth:true}},
	 {path:"refund",name:"退款申请",component:Refund,meta:{requireAuth:true}},
    ]
  },
  {
    path: '/about',
    name: 'About',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  }
]

const router = new VueRouter({
  routes
})
// 全局守卫（每个页面进入前都会执行这个回调函数）
router.beforeEach((to,from,next)=>{
	// 如果也的元信息需要权限
	 if(to.meta.requireAuth){
		 // 获取到用户的id
		 if(localStorage.getItem("userid")){
			 // 获取到直接放行
			 next(true)
		 }else{
			 // 获取不到跳转到登录页面
			 next("/login")
		 }
	 }else{
		 next(true)
	 }
})

export default router
